package com.lenovo.translatetext.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.*;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.lenovo.translatetext.R;
import com.lenovo.translatetext.ui.adapter.LanguageListAdapter;
import com.lenovo.translatetext.utils.SpUtils;
import com.lenovo.translatetext.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.lenovo.translatetext.Constant.BAIDU;
import static com.lenovo.translatetext.Constant.GOOGLE;

public class ChooseLanguageActivity extends AppCompatActivity {

    @BindView(R.id.recycle_view)
    RecyclerView languageList;

    private List<String> languageData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_language);
        ButterKnife.bind(this);
        switch (SpUtils.getTranslateEngine(this)) {
            case BAIDU:
                initBaiduData();
                break;
            case GOOGLE:
                initGoogleData();
                break;
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        languageList.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置Adapter
        languageList.setAdapter(new LanguageListAdapter(languageData, this));
        //设置分隔线
        languageList.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL));
        //设置增加或删除条目的动画
        languageList.setItemAnimator(new DefaultItemAnimator());
    }

    //初始化语言列表数据
    private void initBaiduData() {
        languageData.clear();
        Map<String, String> data = Utils.getBaiduLanguageList();
        for (String key : data.keySet()) {
            languageData.add(key);
        }

    }

    private void initGoogleData() {
        languageData.clear();
        Map<String, String> data = Utils.getGoogleLanguageList();
        for (String key : data.keySet()) {
            languageData.add(key);
        }
    }

    @Override
    public void onBackPressed() {
        Toast.makeText(this, "请选择一种语言", Toast.LENGTH_SHORT).show();
    }
}
